package com.msblearn.C_002;

import java.util.concurrent.TimeUnit;

/**
 * 模拟脏读
 * 对业务写方法加锁。
 * 对业务读不加锁。
 * 这样行不行。
 *
 * 容易产生脏读
 *
 */
public class C06_Account {
    private Boolean balanced= false;
    private Integer data;

    public synchronized void write(boolean b,int data) throws InterruptedException {
        this.data = data;
        Thread.sleep(5000);
        this.balanced = b;
    }

    public boolean read(){
        return this.balanced;
    }

    public static void main(String[] args) {
        C06_Account c06_account = new C06_Account();
        new Thread(()->{
            try {
                c06_account.write(true,100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        System.out.println("第一次读："+c06_account.read());
        try {
            TimeUnit.SECONDS.sleep(7);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(c06_account.read());
    }

}
